﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Openfree.Workflow.Common;

public partial class SiteAdmin_WorkflowOwner : SiteAdminBase
{
    private Control control;
    private DataSet dsState;
    private DataSet dsImage;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitData();

            string doAction = Request["Do"];
            if (!string.IsNullOrEmpty(doAction))
            {
                if ("Save".Equals(doAction))
                {
                    TipMessage1.ShowMessage("保存成功", 5000);
                }

                if ("Submit".Equals(doAction))
                {
                    TipMessage1.ShowMessage("申请成功", 5000);
                }
            }
        }
    }

    private void InitData()
    {
        pnlGrid.Visible = true;

        dsState = BaseAccess.GetDataSet(base.Connection,
            "select Subject, ContainerName from T_SecurityRule where Category='WORKFLOW'");
        dsImage = WorkflowAccess.LoadWorkflowImage();
        DataSet ds = WorkflowManager.LoadOwnerWorkflow(SessionManager.UserID);

        gridView.DataSource = ds.Tables[0];
        gridView.DataBind();
    }

    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            DataRow[] rows = dsImage.Tables[0].Select("ForeignKey='" + ((HiddenField)e.Row.Cells[0].Controls[1]).Value + "'");
            if (rows.Length > 0)
            {
                ((HyperLink)e.Row.Cells[1].Controls[1]).NavigateUrl = "~/PMSWeb/Photo/Workflow/" + rows[0]["FileName"].ToString();
            }

            e.Row.Cells[5].Text = WorkflowAccess.GetWorkfolwStateName(e.Row.Cells[5].Text);
            e.Row.Cells[6].Text = base.FormatBoolean(e.Row.Cells[6].Text, "已提交", "<span style='color:red'>未提交<span>");

            rows = dsState.Tables[0].Select("ContainerName='" + e.Row.Cells[7].Text + "'");
            if (rows.Length > 0)
            {
                e.Row.Cells[7].Text = rows[0]["Subject"].ToString() + "审批";
            }
            else if ("InitialState".Equals(e.Row.Cells[7].Text))
            {
                e.Row.Cells[7].Text = "草稿";
            }
        }
    }

    protected void gridView_SplitPageIndexChange(object sender, CommandEventArgs e)
    {

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("WorkflowAdd.aspx");
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        string instanceId = Request["InstanceId"];
        if (string.IsNullOrEmpty(instanceId))
        {
            TipMessage1.ShowMessage("请选择一条记录", 5000);
            InitData();
            return;
        }

        string cmdText = "select IsSubmit from T_Workflow where InstanceId='{0}'";
        cmdText = string.Format(cmdText, instanceId);
        string isSubmit = BaseAccess.ExecuteScalar(base.Connection, cmdText);
        if ("False".Equals(isSubmit))
        {
            InitEditViewWorkflow(instanceId, "Edit");
        }
        else
        {
            TipMessage1.ShowMessage("修改失败!只有未提交的状态可以修改", 5000);
            InitData();
        }
        
    }

    protected void btnRemove_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Request["InstanceId"]))
        {
            TipMessage1.ShowMessage("请选择一条记录", 5000);
            InitData();
            return;
        }

        string instanceId = Request["InstanceId"];
        if (string.IsNullOrEmpty(instanceId))
        {
            TipMessage1.ShowMessage("请选择一条记录", 5000);
            InitData();
            return;
        }

        string cmdText = "select IsSubmit from T_Workflow where InstanceId='{0}'";
        cmdText = string.Format(cmdText, instanceId);
        string isSubmit = BaseAccess.ExecuteScalar(base.Connection, cmdText);
        if ("False".Equals(isSubmit))
        {
            WorkflowManager.TerminateWolkflow(new Guid(instanceId));

            cmdText = @"delete from T_Workflow where InstanceID='{0}';
            delete from T_WorkflowProcess where InstanceID='{0}'";
            cmdText = string.Format(cmdText, instanceId);
            BaseAccess.ExecuteNonQuery(base.Connection, cmdText);
            
            TipMessage1.ShowMessage("删除成功!", 5000);
            InitData();
            
        }
        else
        {
            TipMessage1.ShowMessage("删除失败!只有未提交的可以删除", 5000);
            InitData();
        }
        
    }
    
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string instanceId = Request["InstanceId"];
        if (string.IsNullOrEmpty(instanceId))
        {
            TipMessage1.ShowMessage("请选择一条记录", 5000);
            InitData();
            return;
        }

        string cmdText = "select IsSubmit from T_Workflow where InstanceId='{0}'";
        cmdText = string.Format(cmdText, instanceId);
        string isSubmit = BaseAccess.ExecuteScalar(base.Connection, cmdText);
        if ("False".Equals(isSubmit))
        {
            InitEditViewWorkflow(instanceId, "Submit");
        }
        else
        {
            TipMessage1.ShowMessage("提交失败!只有未提交的状态可以提交", 5000);
            InitData();
        }
    }

    protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        InitEditViewWorkflow(e.CommandArgument.ToString(), "View");
    }

    private void InitEditViewWorkflow(string instanceID, string doAction)
    {
        string cmdText = @"select a.*, 
                    (select Username from T_User where UserID=a.UserID) Username,
                    (select top 1 CodeName from T_CodeConfig where GroupCode=b.GroupCode and Category='WorkflowGroup') GroupCodeName,
                    b.Subject CategorySubject,
                    b.ActivityPage
                    from T_Workflow a left join T_WorkflowCategory b 
                    on a.CategoryCode=b.CategoryCode where a.InstanceID='{0}'";
        DataSet ds = BaseAccess.GetDataSet(base.Connection, string.Format(cmdText, instanceID));
        DataRow row = ds.Tables[0].Rows[0];

        string url = ds.Tables[0].Rows[0]["ActivityPage"].ToString() +
            "?CategoryCode=" + row["CategoryCode"] + "&InstanceID=" + instanceID + "&Do=" + doAction;
        Response.Redirect(url);
    }
}
